﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ExpressionMapper
{
    /// <summary>
    /// Extracts all fields for mapping.
    /// </summary>
    public class AllFieldsExtractor : IMemberExtractor
    {
        #region IMemberExtractor Members

        public IEnumerable<MappingMember> GetFromMembers(Type t, IEnumerable<MappingMember> toMembers)
        {
            return t.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic |
                               BindingFlags.Instance)
                .Select(f => new MappingMember(f.Name, f.FieldType, f));
        }

        public IEnumerable<MappingMember> GetToMembers(Type t)
        {
            return t.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic |
                               BindingFlags.Instance).Where(f => !f.IsInitOnly)
                .Select(f => new MappingMember(f.Name, f.FieldType, f));
        }

        #endregion
    }
}